package com.ruoyi.analysis.service.analysis;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.analysis.domain.ProductAnalysisTeam;
import com.ruoyi.analysis.domain.analysis.ProductAnalysisTeamAnalysis;
import com.ruoyi.analysis.domain.analysis.ProductAnalysisTeamAnalysisBar;
import com.ruoyi.analysis.domain.analysis.ProductAnalysisTeamAnalysisLine;
import com.ruoyi.analysis.domain.analysis.ProductAnalysisTeamAnalysisPie;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 班组生产数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-04
 */
@Service
public class ProductAnalysisTeamAnalysisService {


    public ProductAnalysisTeamAnalysis analysis(List<ProductAnalysisTeam> list){
        ProductAnalysisTeamAnalysis productAnalysisTeamAnalysis = new ProductAnalysisTeamAnalysis();
        productAnalysisTeamAnalysis.setDeviceRate(analysisDeviceRate(list));
        productAnalysisTeamAnalysis.setWorkRate(analysisWorkRate(list));
        productAnalysisTeamAnalysis.setWeldingUse(analysisWeldingUse(list));
        productAnalysisTeamAnalysis.setElectUse(analysisElectUse(list));
        productAnalysisTeamAnalysis.setGasUse(analysisGasUse(list));
        productAnalysisTeamAnalysis.setWeldTimeAndNum(analysisWeldTimeAndNum(list));
        return productAnalysisTeamAnalysis;
    }


    /**
     * 设备利用率
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisLine analysisDeviceRate(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisLine productAnalysisTeamAnalysisLine = new ProductAnalysisTeamAnalysisLine();
        productAnalysisTeamAnalysisLine.setTitleText("设备利用率");
        productAnalysisTeamAnalysisLine.setLegend(Arrays.asList("设备总数","实焊设备"));
        productAnalysisTeamAnalysisLine.setXAxis(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisLine.setSeries1Name("设备总数");
        productAnalysisTeamAnalysisLine.setSeries1Data(list.stream().map(ProductAnalysisTeam::getDeviceTotal).collect(Collectors.toList()));
        productAnalysisTeamAnalysisLine.setSeries2Name("实焊设备");
        productAnalysisTeamAnalysisLine.setSeries2Data(list.stream().map(ProductAnalysisTeam::getDeviceWork).collect(Collectors.toList()));
        return productAnalysisTeamAnalysisLine;
    }


    /**
     * 工作效率
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisLine analysisWorkRate(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisLine productAnalysisTeamAnalysisLine = new ProductAnalysisTeamAnalysisLine();
        productAnalysisTeamAnalysisLine.setTitleText("工作效率");
        productAnalysisTeamAnalysisLine.setLegend(Arrays.asList("开机时间","加工时间"));
        productAnalysisTeamAnalysisLine.setXAxis(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisLine.setSeries1Name("开机时间");
        productAnalysisTeamAnalysisLine.setSeries1Data(list.stream().map(ProductAnalysisTeam::getWorkTimeStart).collect(Collectors.toList()));
        productAnalysisTeamAnalysisLine.setSeries2Name("加工时间");
        productAnalysisTeamAnalysisLine.setSeries2Data(list.stream().map(ProductAnalysisTeam::getWorkTimeTotal).collect(Collectors.toList()));
        return productAnalysisTeamAnalysisLine;
    }


    /**
     * 焊丝消耗(KG)
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisPie analysisWeldingUse(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisPie productAnalysisTeamAnalysisPie = new ProductAnalysisTeamAnalysisPie();
        productAnalysisTeamAnalysisPie.setTitleText("焊丝消耗(KG)");
        productAnalysisTeamAnalysisPie.setLegend(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisPie.setSeriesName("焊丝消耗(KG)");
        List<JSONObject> data = new ArrayList<>();
        for(ProductAnalysisTeam productAnalysisTeam : list){
            JSONObject jsonObject = new JSONObject(true);
            jsonObject.put("value", productAnalysisTeam.getWeldingUse());
            jsonObject.put("name", productAnalysisTeam.getTeam());
            data.add(jsonObject);
        }
        productAnalysisTeamAnalysisPie.setSeriesData(data);
        return productAnalysisTeamAnalysisPie;
    }


    /**
     * 电能消耗(KWH)
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisPie analysisElectUse(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisPie productAnalysisTeamAnalysisPie = new ProductAnalysisTeamAnalysisPie();
        productAnalysisTeamAnalysisPie.setTitleText("电能消耗(KWH)");
        productAnalysisTeamAnalysisPie.setLegend(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisPie.setSeriesName("电能消耗(KWH)");
        List<JSONObject> data = new ArrayList<>();
        for(ProductAnalysisTeam productAnalysisTeam : list){
            JSONObject jsonObject = new JSONObject(true);
            jsonObject.put("value", productAnalysisTeam.getElectUse());
            jsonObject.put("name", productAnalysisTeam.getTeam());
            data.add(jsonObject);
        }
        productAnalysisTeamAnalysisPie.setSeriesData(data);
        return productAnalysisTeamAnalysisPie;
    }

    /**
     * 气体消耗(L)
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisPie analysisGasUse(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisPie productAnalysisTeamAnalysisPie = new ProductAnalysisTeamAnalysisPie();
        productAnalysisTeamAnalysisPie.setTitleText("气体消耗(L)");
        productAnalysisTeamAnalysisPie.setLegend(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisPie.setSeriesName("气体消耗(L)");
        List<JSONObject> data = new ArrayList<>();
        for(ProductAnalysisTeam productAnalysisTeam : list){
            JSONObject jsonObject = new JSONObject(true);
            jsonObject.put("value", productAnalysisTeam.getGasUse());
            jsonObject.put("name", productAnalysisTeam.getTeam());
            data.add(jsonObject);
        }
        productAnalysisTeamAnalysisPie.setSeriesData(data);
        return productAnalysisTeamAnalysisPie;
    }

    /**
     * 焊接时间和焊接数量
     * @param list：
     * @return ：
     */
    private ProductAnalysisTeamAnalysisBar analysisWeldTimeAndNum(List<ProductAnalysisTeam> list) {
        ProductAnalysisTeamAnalysisBar productAnalysisTeamAnalysisBar = new ProductAnalysisTeamAnalysisBar();
        productAnalysisTeamAnalysisBar.setTitleText("焊接时间和焊接数量");
        productAnalysisTeamAnalysisBar.setLegend(Arrays.asList("焊接时间","焊接数量"));
        productAnalysisTeamAnalysisBar.setXAxis(list.stream().map(ProductAnalysisTeam::getTeam).collect(Collectors.toList()));
        productAnalysisTeamAnalysisBar.setSeries1Name("焊接时间");
        productAnalysisTeamAnalysisBar.setSeries1Data(list.stream().map(ProductAnalysisTeam::getWorkTimeTotal).collect(Collectors.toList()));
        productAnalysisTeamAnalysisBar.setSeries2Name("焊接数量");
        productAnalysisTeamAnalysisBar.setSeries2Data(list.stream().map(ProductAnalysisTeam::getWeldingNum).collect(Collectors.toList()));
        return productAnalysisTeamAnalysisBar;
    }


}
